package cn.yunhe.service.impl;


import cn.yunhe.mapper.DeptPMapper;
import cn.yunhe.mapper.ModulePMapper;
import cn.yunhe.mapper.UserInfoPMapper;
import cn.yunhe.mapper.UserpMapper;
import cn.yunhe.pojo.CurrentUser;
import cn.yunhe.pojo.ModuleP;
import cn.yunhe.pojo.Userp;
import cn.yunhe.pojo.UserpExample;
import cn.yunhe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserpMapper userPMapper;
    @Autowired
    private DeptPMapper deptPMapper;
    @Autowired
    private UserInfoPMapper userInfoPMapper;
    @Autowired
    private ModulePMapper modulePMapper;

    @Override
    public Userp login(String username) {
        UserpExample userPExample = new UserpExample();
        UserpExample.Criteria criteria = userPExample.createCriteria();

        criteria.andUserNameEqualTo(username);

        //必须是启动状态
        criteria.andStateEqualTo(1);


        List<Userp> users = userPMapper.selectByExample(userPExample);
        if(users!=null && users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public CurrentUser findUserInfoDeptByUser(Userp user) {
        CurrentUser currentUser = new CurrentUser();
        currentUser.setUserP(user);// 设置用户基本信息
        currentUser.setDeptP(deptPMapper.selectByPrimaryKey(user.getDeptId()));// 设置用户所属部门信息
        currentUser.setUserInfoP(userInfoPMapper.selectByPrimaryKey(user.getUserId()));// 设置用户详细信息

        return currentUser;
    }

    @Override
    public List<ModuleP> getPermissionsByUserId(String userid) {
        return modulePMapper.getPermissionsByUserId(userid);
    }


}
